package com.xcity.game.combat.skill.impl;

import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.ActionEffect.Value;
import com.xcity.game.combat.Round;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.skill.CombatSkill;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.skill.Skill;

/**
 * 亡羊补牢<br/>
 * 猪也不能丢，给自己增加一个等同于死亡队友数量x{0=100#0}+自身剩余气势的护盾，护盾破碎时自身获得{1=100#0}%攻击提升，持续{2=1#0}回合。
 * @author yang.li
 *
 */
public class COMBAT_SKILL_90111 extends CombatSkill {

	public COMBAT_SKILL_90111(Skill skill) {
		super(skill);
	}

	@Override
	public void calcDamage(Action action) {
		CombatUnit source = action.unit;
		int n = (int) skill.getParameter(0);
		int hp = source.getTeam().sizeOfDead() * n + source.getHp();
		action.addBuff(new BUFF(skill, source, hp));
	}
	
	static class BUFF extends CombatBuff {
		Skill skill;
		int hp;
		public BUFF(Skill skill, CombatUnit owner, int hp) {
			super(skill.getTemplate().getCombatBuff(), owner);
			this.hp = hp;
			this.skill = skill;
		}
		@Override
		public void preDamage(ActionEffect effect) {
			int delta = effect.getHpDelta();
			if (delta < 0 && hp > 0) {
				hp += delta;
				Value v = effect.getValue(CombatAttributeType.HP);
				v.setDelta(hp > 0 ? 0 : hp);
			}
		}
		@Override
		public void afterAction(Action action) {
			if (hp <= 0 && state == STATE_ADDED) { // 破盾
				uneffect();
				action.ref().addBuff(this);
				action.addBuff(new BUFF2(skill, owner));
			}
		}
		@Override
		public void roundEnd(Round round) {
//			if (hp <= 0) { // 破盾
//				uneffect();
//				round.addBuff(this);
//			}
		}
	}
	
	static class BUFF2 extends CombatBuff {
		int oldAttackPower;
		public BUFF2(Skill skill, CombatUnit owner) {
			super(skill.getTemplate().getCombatBuff(1), owner, (int) skill.getParameter(2));
			active = true;
			int power = (int) owner.getAttackPower();
			oldAttackPower = power;
			power += power * skill.getParameter(1);
			owner.setAttributeValue(CombatAttributeType.ATTACK_POWER, power);
		}
		@Override
		public void roundEnd(Round round) {
			super.roundEnd(round);
			if (state == STATE_REMOVED) {
				owner.setAttributeValue(CombatAttributeType.ATTACK_POWER, oldAttackPower);
			}
		}
	}

}
